package command

import (
	"errors"
	"ssadmin/common/utils"
	"ssadmin/internal/admin/models"
	"strings"

	uuid "github.com/satori/go.uuid"
)

type VerifyCodeReq struct {
	Identifier string `form:"identifier" comment:"identifier"`
	MsgCode    string `json:"-"`
	Data       string `json:"-"`
}

type PasswordForgetResetReq struct {
	Code     string `json:"code" comment:"code"`
	Mobile   string `json:"mobile" comment:"mobile"`
	Password string `json:"password" comment:"password"`
	UserName string `json:"userName" comment:"userName"`
}

type RegisterInsertReq struct {
	Id       string `json:"-" comment:"用户id"` // 用户id
	Alias    string `json:"alias" comment:"alias"`
	Code     string `json:"code" comment:"code"`
	Company  string `json:"company" comment:"company"`
	Email    string `json:"email" comment:"email"`
	Mobile   string `json:"mobile" comment:"mobile"`
	Password string `json:"password" comment:"password"`
	UserName string `json:"userName" comment:"userName"`

	CreatorId string `json:"-"`
	Creator   string `json:"-"`
}

func (s *RegisterInsertReq) Generate() error {
	if len(s.Code) == 0 {
		return errors.New("验证码不能为空")
	}

	if len(s.UserName) < 6 {
		return errors.New("登录账号至少6个英文字符或2个中文字符")
	}

	if len(s.Password) == 0 {
		return errors.New("密码不能为空")
	}

	if len(s.Company) == 0 {
		return errors.New("公司名称不能为空")
	}

	if len(s.Mobile) == 0 {
		return errors.New("手机号不能为空")
	}

	s.Password = utils.NewUserDefaultPwd(s.UserName, s.Password)

	return nil
}

func (s *RegisterInsertReq) GetId() interface{} {
	return s.Id
}

type RegisterUpdateReq struct {
	Id string `form:"Id" comment:"用户id"` // 用户id
}

func (s *RegisterUpdateReq) Generate(model *models.Register) {
	if len(s.Id) == 0 {
		model.Id = uuid.NewV4().String()
	} else {
		model.Id = s.Id
	}
}

func (s *RegisterUpdateReq) GetId() interface{} {
	return s.Id
}

// RegisterGetReq 功能获取请求参数
type RegisterGetReq struct {
	Id       string `uri:"id" search:"type:exact;column:id;table:t_register"`
	UserName string `uri:"username" search:"type:exact;column:user_name;table:t_register"`
}

func (s *RegisterGetReq) GetId() interface{} {
	return s.Id
}

// RegisterDeleteReq 功能删除请求参数
type RegisterDeleteReq struct {
	Ids []string `form:"Ids"`
}

type RegisterRole struct {
	Id     string
	RoleId string
}
type RegisterDept struct {
	Id     string
	DeptId string
}

func (s *RegisterDeleteReq) GetId() []string {
	s.Ids = strings.Split(s.Ids[0], ",")
	return s.Ids
}

// RejectRegistReq 功能获取请求参数
type RejectRegistReq struct {
	Id     string `uri:"id" search:"type:exact;column:register_id;table:t_register"`
	Ids    string `uri:"ids" search:"type:exact;column:register_id;table:t_register"`
	Status string `uri:"-" search:"-"`
}

func (s *RejectRegistReq) GetId() []string {
	if len(s.Id) > 0 {
		return strings.Split(s.Id, ",")
	}
	if len(s.Ids) > 0 {
		return strings.Split(s.Ids, ",")
	}
	return nil
}
